package com.erpoa.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author chenjimin
 * @Title: MailUtil
 * @ProjectName oaerp
 * @Description: TODO
 * @date 2020/6/1620:14
 */
@Component
public class MailUtil {

    @Autowired
    private JavaMailSender mailSender; // 自动注入的Bean

    @Value("${spring.mail.username}")
    private String sender; // 读取配置文件中的参数

    @Value("${spring.mail.usernamebcc}")
    private String senderBCC; // 读取配置文件中的参数
    @Value("${spring.mail.usernamecc}")
    private String senderBCC1; // 读取配置文件中的参数

    /**
     * 发送邮件测试方法
     */
    public void sendMail(String senderto,String subject,String text) {
        SimpleMailMessage mimeMessage = new SimpleMailMessage();
        mimeMessage.setFrom(sender);
        mimeMessage.setTo(senderto);
        mimeMessage.setBcc(senderBCC);
        mimeMessage.setCc(senderBCC1);

        mimeMessage.setSubject(subject);
        mimeMessage.setText(text);
        mailSender.send(mimeMessage);
    }

    /**
     * 发送简易邮件
     *
     * @param mailBean
     */
    public void sendMail(MailBean mailBean,String senderto) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);

        try {
            helper.setFrom(sender);
            helper.setTo(senderto);
            helper.setSubject(mailBean.getSubject());
            helper.addBcc(senderBCC);
           helper.addCc(senderBCC1);
            helper.addCc("erp@mg-pen.com");

            helper.setText(mailBean.getText());
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        mailSender.send(mimeMessage);
    }

    /**
     * 发送邮件-邮件正文是HTML
     *
     * @param mailBean
     */
    public void sendMailHtml(MailBean mailBean,String senderto) {

        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(sender);
            helper.setTo(senderto);
            helper.addBcc(senderBCC);
            helper.addCc(senderBCC1);
            helper.addCc("yl@mg-pen.com");
            helper.addCc("wujinqing@mg-pen.com");
            helper.addCc("wangxuefeng@mg-pen.com");
            helper.addCc("erp@mg-pen.com");
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getText(), true);
            // 增加附件名称和附件
            // helper.addAttachment(mailBean.getAttachmentFilename(), mailBean.getFile());
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }


    }


    /**
     * 发送邮件-邮件正文是HTML
     *
     * @param mailBean
     */
    public void sendMailHtmlmq(MailBean mailBean, String senderto) {

        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(sender);
            helper.setTo(senderto);
            helper.addBcc(senderBCC);
            helper.addCc(senderBCC1);
            helper.addCc("chenjimin@mg-pen.com");
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getText(), true);
            // 增加附件名称和附件
            // helper.addAttachment(mailBean.getAttachmentFilename(), mailBean.getFile());
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }


    }

    /**
     * 发送邮件-附件邮件
     *
     * @param mailBean
     */
    public void sendMailAttachment(MailBean mailBean,String senderto) {
        try {

            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(sender);
            helper.setTo(senderto);
            // helper.addBcc(senderBCC);
            // helper.addCc(senderBCC1);
            // helper.addCc("erp@mg-pen.com");
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getText(), true);
            // 增加附件名称和附件
            helper.addAttachment(mailBean.getAttachmentFilename(), mailBean.getFile());
            if (!"45@mg-pen.com".equals(senderto)) {
                mailSender.send(mimeMessage);
            }
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 内联资源（静态资源）邮件发送
     * 由于邮件服务商不同，可能有些邮件并不支持内联资源的展示
     * 在测试过程中，新浪邮件不支持，QQ邮件支持
     * 不支持不意味着邮件发送不成功，而且内联资源在邮箱内无法正确加载
     *
     * @param mailBean
     */
    public void sendMailInline(MailBean mailBean,String senderto) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(sender);
            helper.setTo(senderto);
            helper.setSubject(mailBean.getSubject());
            helper.addBcc(senderBCC);
            helper.addCc(senderBCC1);
            helper.addCc("erp@mg-pen.com");
            /*
             * 内联资源邮件需要确保先设置邮件正文，再设置内联资源相关信息
             */
            helper.setText(mailBean.getText(), true);
            helper.addInline(mailBean.getContentId(), mailBean.getFile());

            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

    /**
     * 模板邮件发送
     *
     * @param mailBean
     */
    public void sendMailTemplate(MailBean mailBean,String senderto) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(sender);
            helper.setTo(senderto);
            helper.addBcc(senderBCC);
            helper.addCc(senderBCC1);
            helper.addCc("erp@mg-pen.com");
            helper.setSubject(mailBean.getSubject());
            helper.setText(mailBean.getText(), true);
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}

